package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FamMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FamTreeMapper;
import com.ruoyi.system.domain.FamTree;
import com.ruoyi.system.service.IFamTreeService;
//import com.ruoyi.system.domain.treeSelect;
/**
 * 家谱图Service业务层处理
 * 
 * @author wk
 * @date 2022-05-22
 */
@Service
public class FamTreeServiceImpl implements IFamTreeService 
{
    @Autowired
    private FamTreeMapper famTreeMapper;

    /**
     * 查询家谱图
     * 
     * @param memberId 家谱图主键
     * @return 家谱图
     */
    @Override
    public FamTree selectFamTreeByMemberId(Long memberId)
    {
        return famTreeMapper.selectFamTreeByMemberId(memberId);
    }

    /**
     * 查询家谱图列表
     * 
     * @param famTree 家谱图
     * @return 家谱图
     */
    @Override
    public List<FamTree> selectFamTreeList(FamTree famTree)
    {
        return famTreeMapper.selectFamTreeList(famTree);
    }

    /**
     * 新增家谱图
     * 
     * @param famTree 家谱图
     * @return 结果
     */
    @Override
    public int insertFamTree(FamTree famTree)
    {
        return famTreeMapper.insertFamTree(famTree);
    }

    /**
     * 修改家谱图
     * 
     * @param famTree 家谱图
     * @return 结果
     */
    @Override
    public int updateFamTree(FamTree famTree)
    {
        return famTreeMapper.updateFamTree(famTree);
    }

    /**
     * 批量删除家谱图
     * 
     * @param memberIds 需要删除的家谱图主键
     * @return 结果
     */
    @Override
    public int deleteFamTreeByMemberIds(Long[] memberIds)
    {
        return famTreeMapper.deleteFamTreeByMemberIds(memberIds);
    }
    /**
     * 得到子节点列表
     */
    private List<FamMember> getChildList(List<FamMember> list, FamMember t)
    {
        List<FamMember> tlist = new ArrayList<FamMember>();
        Iterator<FamMember> it = list.iterator();
        while (it.hasNext())
        {
            FamMember n = (FamMember) it.next();
            if (StringUtils.isNotNull(n.getFatherId()) && n.getFatherId().longValue() == t.getFatherId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 删除家谱图信息
     * 
     * @param memberId 家谱图主键
     * @return 结果
     */
    @Override
    public int deleteFamTreeByMemberId(Long memberId)
    {
        return famTreeMapper.deleteFamTreeByMemberId(memberId);
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<FamMember> list, FamMember t)
    {
        return getChildList(list, t).size() > 0;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<FamMember> list, FamMember t)
    {
        // 得到子节点列表
        List<FamMember> childList = getChildList(list, t);
        t.setChildren(childList);
        for (FamMember tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 构建前端所需要树结构
     *
     * @param members 成员列表
     * @return 树结构列表
     */
    @Override
    public List<FamMember> buildMemberTree(List<FamMember> members)
    {
        List<FamMember> returnList = new ArrayList<FamMember>();
        List<Long> tempList = new ArrayList<Long>();
        for (FamMember member : members)
        {
            tempList.add(member.getMemberId());
        }
        for (FamMember member : members)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(member.getFatherId()))
            {
                recursionFn(members, member);
                returnList.add(member);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = members;
        }
        return returnList;
    }
    /**
     * 构建前端所需要下拉树结构
     *
     * @param members 部门列表
     * @return 下拉树结构列表
     */
//    @Override
//    public List<TreeSelect> buildMemberTreeSelect(List<FamMember> members)
//    {
//        List<FamMember> memberTrees = buildMemberTree(members);
//        return memberTrees.stream().map(treeSelect::new).collect(Collectors.toList());
//    }
}
